package com.ruoyi.web.controller.marry;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.marry.domain.TMarryImage;
import com.ruoyi.marry.service.ITMarryImageService;
import com.ruoyi.marry.util.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 其他照片Controller
 *
 * @author user_marry
 * @date 2020-11-19
 */
@RestController
@RequestMapping("/marry/marryOtherImage")
public class TMarryOtherImageController extends BaseController {

    @Autowired
    private ITMarryImageService tMarryImageService;

    @PreAuthorize("@ss.hasPermi('marry:marryImage:list')")
    @GetMapping("/list")
    public AjaxResult marryOtherImager(TMarryImage tMarryImage) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNull(tMarryImage.getLoversId())) {
            tMarryImage.setLoversId(CommonConstant.getLoversId());
        }
        map.put("loversId", tMarryImage.getLoversId());
        //获取小程序封面照片
        tMarryImage.setType(Constants.TYPE_002);
        List<TMarryImage> list002 = tMarryImageService.selectTMarryImageList(tMarryImage);
        if (StringUtils.isNotEmpty(list002)) {
            TMarryImage image002 = list002.get(0);
            map.put("image002", image002);
        } else {
            map.put("image002", new TMarryImage());
        }

        //获取首页海报照片
        tMarryImage.setType(Constants.TYPE_003);
        List<TMarryImage> list003 = tMarryImageService.selectTMarryImageList(tMarryImage);
        if (StringUtils.isNotEmpty(list003)) {
            TMarryImage image003 = list003.get(0);
            map.put("image003", image003);
        } else {
            map.put("image003", new TMarryImage());
        }

        //获取分享页照片
        tMarryImage.setType(Constants.TYPE_004);
        List<TMarryImage> list004 = tMarryImageService.selectTMarryImageList(tMarryImage);
        if (StringUtils.isNotEmpty(list004)) {
            TMarryImage image004 = list004.get(0);
            map.put("image004", image004);
        } else {
            map.put("image004", new TMarryImage());
        }
        return AjaxResult.success(map);
    }

    /**
     * 提交其他照片信息
     */
    @PreAuthorize("@ss.hasPermi('marry:marryImage:submit')")
    @Log(title = "其他照片", businessType = BusinessType.OTHER)
    @PostMapping
    @RepeatSubmit
    public AjaxResult submit(@RequestBody Map<String, TMarryImage> map) {
        int count = 0;
        int seq = 2;

        // type="002"
        TMarryImage image002 = map.get("image002");
        if (StringUtils.isNotNull(image002)) {
            if (StringUtils.isNull(image002.getId())) {
                image002.setSeq(seq);
                image002.setType(Constants.TYPE_002);
                count = tMarryImageService.insertTMarryImage(image002);
            } else {
                count = tMarryImageService.updateTMarryImage(image002);
            }
        }
        // type="003"
        TMarryImage image003 = map.get("image003");
        if (StringUtils.isNotNull(image003)) {
            if (StringUtils.isNull(image003.getId())) {
                image003.setSeq(seq);
                image003.setType(Constants.TYPE_003);
                count = tMarryImageService.insertTMarryImage(image003);
            } else {
                count = tMarryImageService.updateTMarryImage(image003);
            }
        }
        // type="004"
        TMarryImage image004 = map.get("image004");
        if (StringUtils.isNotNull(image004)) {
            if (StringUtils.isNull(image004.getId())) {
                image004.setSeq(seq);
                image004.setType(Constants.TYPE_004);
                count = tMarryImageService.insertTMarryImage(image004);
            } else {
                count = tMarryImageService.updateTMarryImage(image004);
            }
        }
        return toAjax(count);
    }

}
